﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace AutoIE
{
    class SimpleProcessor : BasicProcessor
    {
        public override void Run()
        {
            //var url = @"http://www.sdu.edu.cn/";
            ////OutputLine("Recording...");
            //StartRecord(url,"Rec", true);
            ////Data.MoveFirst();
            ////while (Data.MoveNext())
            ////{
            ////    var p = WaitUrl(@"d:\ch\projects\boson\autoie\test\login\login.htm");
            ////    var m1 = new string[] { Data["用户"], "uid", Data["密码"], "psw", Data["性别"], "sex", Data["爱好"], "fav", Data["籍贯"], "home", Data["备注"], "note"};
            ////    p.Fill(m1);
            ////    Wait(3000);
            ////    p.Click("login");
            ////    var pa = WaitAnyUrl(new string[] { @"d:\ch\projects\boson\autoie\test\login\loginsuc.htm", @"d:\ch\projects\boson\autoie\test\login\loginfail.htm" });
            ////    if (pa.Url.IndexOf("loginfail.htm") > 0)
            ////    {
            ////        Data.Result = "失败";
            ////        OutputLine(Data["用户"] + ":" + Data["密码"] + " 失败");
            ////        GoUrl(url);
            ////        continue;
            ////    }
            ////    else if (pa.Url.IndexOf("loginsuc.htm") > 0)
            ////    {
            ////        Data.Result = "成功";
            ////        OutputLine(Data["用户"] + ":" + Data["密码"] + " 成功");
            ////        break;
            ////    }
            ////}
            ////Data.Save();
            var url = "http://www.sdu.edu.cn/";
            Initialize(url, "s.xlsx", true);
            Data.MoveFirst();
            do
            {
                var p1 = GotoUrl(url);
                var vcode = "123";// Recognise(p1.GetImage("CODE"));
                var m1 = new object[] { Data["用户"], "uid", Data["密码"], "password", vcode, "verifyCode" };
                p1.Fill(m1);
                p1.Click("enter");
                var p2 = WaitAnyUrl(new string[] { 
                    "^http://sdu.edu.cn/coremail/xjs/index.jsp\\?sid=.*$"
                    , "http://sdu.edu.cn/coremail/login.jsp"
                    , "^http://sdu.edu.cn/coremail/loginVC.jsp\\?sid=.*$" });
                if (p2.Url.IndexOf("login") > 0)
                {
                    Data["结果"] = "失败";
                    OutputLine(Data["用户"] + " : " + Data["密码"] + "-> 失败");
                    Wait(1000); // 等待一秒
                    continue;
                }
                else
                {
                    Data["结果"] = "成功";
                    OutputLine(Data["用户"] + ":" + Data["密码"] + "-> 成功-> 结束。");
                    break;
                }
            } while (Data.MoveNext()) ;
            Data.Save();
        }
    }
}
